//
//  SNThemeActsListCell.m
//  app
//
//  Created by 王少宁 on 15/8/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNThemeActsListCell.h"

@implementation SNThemeActsListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier shopBaseModel:(SNShopBaseModel *)model
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier shopBaseModel:nil]) {
        if (model != nil) {
            [self cellWithModel:model];
        }
    }
    return self;
}

- (void)cellWithModel:(SNShopBaseModel *)shopModel
{
    SNShopThemeActsListModel * model = (SNShopThemeActsListModel *)shopModel;
    
    if (!model.themeActsListArray.count) return;
    
    self.shopBaseModel = model;
    
    if (self.contentView.subviews.count) {
        [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    CGFloat itemsBackViewH = SCREEN_WIDTH * 425/720 - UP(1);
    
    UIView * itemsBackView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, itemsBackViewH)];
    [self.contentView addSubview:itemsBackView];
    
    CGFloat itemW = (itemsBackView.ubo.width - UP(2)) / 3;
    
    SNShopThemeActsListSubModel * firstItemModel = model.themeActsListArray.firstObject;
    UIView * firstItemBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemW, itemsBackViewH)];
    [itemsBackView addSubview:firstItemBackView];
    
    UILabel * firstTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, itemW - 5, 30)];
    firstTitleLable.font = JKAppGlobleFontSmallTagText;
    firstTitleLable.textColor = [XCommon hexStringToColor:@"#696969"];
    firstTitleLable.text = firstItemModel.name;
    [firstItemBackView addSubview:firstTitleLable];
    
    UIImageView * firstImgView = CREATE_WITH_FRAME_OF(UIImageView, [firstTitleLable.ufo fx:0 fy:1.0],
                                                      [firstTitleLable.ubo fw:1.0 fh:-1.0 oh:firstItemBackView.ubo.height - 5]);
    firstImgView.contentMode = UIViewContentModeScaleAspectFit;
    [firstImgView sd_setImageWithURL:[NSURL URLWithString:firstItemModel.pic]];
    [firstItemBackView addSubview:firstImgView];
    
    UIButton * firstItemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    firstItemBtn.frame = firstItemBackView.bounds;
    firstItemBtn.tag = 1;
    [firstItemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [firstItemBackView addSubview:firstItemBtn];
    
    CGFloat subItemH = (itemsBackViewH - UP(1)) * 0.5;
    
    for (int i = 0; i < model.themeActsListArray.count - 1; i++) {
        
        SNShopThemeActsListSubModel * subItemModel = model.themeActsListArray[i + 1];
        
        UIView * subItemBackView = [[UIView alloc] initWithFrame:CGRectMake((i%2 + 1) * (itemW + UP(1)), i/2 * (subItemH + UP(1)), itemW, subItemH)];
        subItemBackView.contentMode = UIViewContentModeScaleAspectFit;
        [itemsBackView addSubview:subItemBackView];
        
        UILabel * subTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, subItemBackView.ubo.width - 10, 30)];
        subTitleLable.font = JKAppGlobleFontSmallTagText;
        switch (i) {
            case 0:
                subTitleLable.textColor = [XCommon hexStringToColor:@"#fa5b6f"];
                break;
                
            case 1:
                subTitleLable.textColor = [XCommon hexStringToColor:@"#872ab8"];
                break;
                
            case 2:
                subTitleLable.textColor = [XCommon hexStringToColor:@"#f57085"];
                break;
                
            case 3:
                subTitleLable.textColor = [XCommon hexStringToColor:@"#f15da2"];
                break;
        }
        subTitleLable.text = subItemModel.name;
        [subItemBackView addSubview:subTitleLable];
        
        UIImageView * subImgView = CREATE_WITH_FRAME_OF(UIImageView, [subTitleLable.ufo fx:0 fy:1.0],
                                                          [subTitleLable.ubo fw:1.0 fh:-1.0 oh:subItemBackView.ubo.height - 5]);
        [subImgView sd_setImageWithURL:[NSURL URLWithString:subItemModel.pic]];
        [subItemBackView addSubview:subImgView];
        
        UIButton * subItemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        subItemBtn.backgroundColor = [UIColor clearColor];
        subItemBtn.frame = subItemBackView.bounds;
        subItemBtn.tag = i + 2;
        [subItemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [subItemBackView addSubview:subItemBtn];
    }
}

- (void)itemBtnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(itemClickWithShopBaseModel:row:index:)]) {
        [self.delegate itemClickWithShopBaseModel:self.shopBaseModel row:0 index:btn.tag - 1];
    }
}

- (void)drawRect:(CGRect)rect {
    CGFloat itemsBackViewH = SCREEN_WIDTH * 425/720 - UP(1);
    CGFloat itemW = (SCREEN_WIDTH - 30 - UP(2)) / 3;
    CGFloat subItemH = (itemsBackViewH - UP(1)) * 0.5;

    [self drawBackground:rect color:[UIColor whiteColor]];
    [self drawVerticalLineTop:0 bottom:5 at:itemW + UP(1) + 15];
    [self drawVerticalLineTop:0 bottom:5 at:(itemW + UP(1)) * 2 + 15];
    [self drawHorizonLineLeft:itemW + 15 right:15 at:subItemH];
}

@end
